-- UIMoneyExchange
-- Create by daisl
-- 金币不足兑换提示框

--UIMoneyExchange继承自Layer
UIMoneyExchange = class("UIMoneyExchange", function()
    return cc.Layer:create()
end)

-- @param msg        提示框的内容
-- @param loss       兑换消耗
-- @param gain       获得金币
-- @param okFunc     点击确认的处理函数
-- @param args       okFunc所需的参数列表（当参数超过1个时，这里通常传入table类型）
function UIMoneyExchange.create(msg, loss, gain, okFunc, args)
    return UIMoneyExchange.new(msg, loss, gain, okFunc, args)
end

-- 内部函数声明
local resize;

-- 构造函数
function UIMoneyExchange:ctor(msg, loss, gain, okFunc, args)
    -- 初始化
    local node = cc.CSLoader:createNode("layout/dialog/MoneyExchange.csb");
    self:addChild(node);

    local panel = findChildByName(node, "img_panel");

    -- 设置内容
    local contentLabel = panel:getChildByName("content");
    contentLabel:setString(msg);

    -- 解析loss gain
    local lossNum;
    for k, var in pairs(loss) do
        if k == 3 then
            lossNum = var;
        end
    end

    local gainNum;
    for j, var in pairs(gain) do
        if j == 1 then
            gainNum = var[3];
        end
    end
    -- 设置兑换比例
    local costBar = panel:getChildByName("cost_bar");
    local lossGem = costBar:getChildByName("amount2");
    lossGem:setString(tostring(lossNum));

    local gainMoney = costBar:getChildByName("amount");
    gainMoney:setString(tostring(gainNum));


    local okBtn = panel:getChildByName("btn_ok");
    -- 注册确认按钮点击事件
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_select");
            UIMgr.getCurrentScene():removeDialog(self);
            if type(okFunc) == 'function' then
                okFunc(args);
            end
        end
    end
    okBtn:addTouchEventListener(onOkClick);

    -- 注册取消按钮点击事件
    local canelBtn = panel:getChildByName("btn_cancel")
    local function onCancelClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeDialog(self);
        end
    end
    AddBackEventHandler(self, canelBtn, onCancelClick);

    -- 设置按钮标题的描边
    TextStyleM.setOutlineStyle(okBtn:getTitleRenderer());
    TextStyleM.setOutlineStyle(canelBtn:getTitleRenderer());

    -- 重新调整布局
    resize(node)
end

-- 重新调整布局
resize = function(node)
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- 确认框居中并缩放到合适大小
    AlignM.alignToCenter(node, "img_panel");
end

return UIMoneyExchange
